<template>
  <div>
    <Categorysearch :disabled="!showList" @categoryChange="categoryChange"></Categorysearch>
    <el-card v-show="showList">
      <template>
        <div>
          <el-button
            style="margin-bottom: 20px"
            type="primary"
            icon="el-icon-plus"
            :disabled="disabled"
            @click="addAttr"
            >添加属性</el-button
          >
          <el-table
            border
            :data="attrInfoList"
            v-loading="loading"
            element-loading-text="拼命加载中..."
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(255, 255, 255, 0.6)"
          >
            <el-table-column
              type="index"
              label="序号"
              align="center"
            ></el-table-column>
            <el-table-column prop="attrName" label="属性名称" width="200">
            </el-table-column>
            <el-table-column prop="attrValueList" label="属性值">
              <template slot-scope="{ row: { attrValueList } }">
                <el-tag
                  type="success"
                  style="margin: 5px"
                  v-for="attr in attrValueList"
                  :key="attr.id"
                >
                  {{ attr.valueName }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="130">
              <template slot-scope="{ row }">
                <Tipbutton
                  icon="el-icon-edit"
                  content="修改"
                  type="primary"
                  size="mini"
                  @click="update(row)"
                ></Tipbutton>
                <el-popconfirm
                  @onConfirm="delAttr(row.id)"
                  :title="`确定删除${row.attrName}吗？`"
                >
                  <template #reference>
                    <Tipbutton
                      :icon="`el-icon-delete`"
                      :content="`删除`"
                      :type="`danger`"
                      :size="`mini`"
                    ></Tipbutton>
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </template>
    </el-card>
    <el-card v-show="!showList">
      <el-form label-width="100px">
        <el-form-item label="属性名称:">
          <el-input
            v-model="attr.attrName"
            placeholder="请输入属性名称"
            suffix-icon="el-icon-edit"
            style="width: 200px"
          >
          </el-input>
          <el-button
            style="margin-left: 20px"
            type="primary"
            icon="el-icon-plus"
            :disabled="attr.attrName === ''"
            @click="add"
            >添加属性值</el-button
          >
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
      <el-table border :data="attr.attrList" style="margin-bottom: 20px">
        <el-table-column type="index" align="center" label="序号" width="100">
        </el-table-column>
        <el-table-column label="属性值">
          <template v-slot="{ row, $index }">
            <el-input
              :ref="$index"
              v-if="toEdit && index === $index"
              v-model="row.valueName"
              @blur="removeFocus(row, $index, $event)"
              @keyup.enter.native="removeFocus(row, $index, $event)"
            ></el-input>
            <span
              v-else
              style="display: inline-block; width: 100%"
              @click="edit($index)"
              >{{ row.valueName }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" width="70" align="center">
          <template v-slot="{ row, $index }">
            <el-popconfirm
              @onConfirm="del($index)"
              :title="`确定删除${row.valueName}吗？`"
            >
              <template #reference>
                <Tipbutton
                  :icon="`el-icon-delete`"
                  :content="`删除`"
                  :type="`danger`"
                  :size="`mini`"
                ></Tipbutton>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-button
        type="primary"
        :disabled="attr.attrList.length === 0"
        @click="save"
        :loading="btnloading"
        >保存</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";
import { nanoid } from "nanoid";
export default {
  name: "AttrList",
  data() {
    return {
      loading: false,
      btnloading: false,
      disabled: true,
      showList: true,
      toEdit: false,
      addOrUpdate: 0,
      index: -1,
      value1: "",
      value2: "",
      value3: "",
      attrInfoList: [],
      attr: {
        id: 0,
        attrName: "",
        attrList: [],
      },
    };
  },
  methods: {
    async categoryChange({ c1, c2, c3 }) {
      this.disabled = true;
      if (c3 === "") {
        this.attrInfoList = [];
        return;
      }
      this.disabled = false;
      this.loading = true;
      this.value1 = c1;
      this.value2 = c2;
      this.value3 = c3;
      try {
        const { data } = await this.$api.getAttrInfoList(c1, c2, c3);
        this.attrInfoList = data;
        this.loading = false;
      } catch (e) {
        this.$message({
          type: "error",
          message: e,
        });
      }
    },
    update(row) {
      this.addOrUpdate = -1;
      this.showList = false;
      const { attrName, attrValueList, id } = row;
      attrValueList.forEach((v) => (v.copyValueName = v.valueName));
      this.attr = cloneDeep({ attrName, attrList: attrValueList, id });
    },
    addAttr() {
      this.showList = false;
      this.addOrUpdate = 1;
    },
    async delAttr(id) {
      this.loading = true;
      try {
        await this.$api.delAttr(id);
        const { value1, value2, value3 } = this;
        const { data } = await this.$api.getAttrInfoList(
          value1,
          value2,
          value3
        );
        this.attrInfoList = data;
      } catch (e) {
      } finally {
        this.loading = false;
      }
    },
    edit(index) {
      this.index = index;
      this.toEdit = true;
      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
    cancel() {
      (this.showList = true), (this.index = -1);
      (this.addOrUpdate = 0),
        (this.attr = {
          id: 0,
          attrName: "",
          attrList: [],
        });
    },
    del(index) {
      this.attr.attrList.splice(index, 1);
    },
    add() {
      const len = this.attr.attrList.push({
        attrId: this.attr.id,
        valueName: "",
        id: Date.now(),
      });
      (this.toEdit = true), (this.index = len - 1);
      this.$nextTick(() => {
        this.$refs[len - 1].focus();
      });
    },
    removeFocus(row, $index, $event) {
      row.hasOwnProperty("copyValueName")
        ? $event.target.value === "" && (row.valueName = row.copyValueName)
        : $event.target.value === ""
        ? this.attr.attrList.splice($index, 1)
        : (row.valueName = row.copyValueName = $event.target.value);
      this.index = -1;
    },
    async save() {
      this.btnloading = true;
      this.attr.attrList.forEach((v) => delete v.copyValueName);
      try {
        await this.$api.save(
          Object.assign(
            {
              categoryLevel: 3,
              categoryId: this.value3,
              attrName: this.attr.attrName,
              attrValueList: this.attr.attrList,
            },
            this.addOrUpdate === 1 ? {} : { id: this.attr.attrList[0].attrId }
          )
        );
        this.$message({
          type: "success",
          duration: 2000,
          message: "保存成功",
        });
        this.btnloading = false;
        this.cancel();
        const { value1, value2, value3 } = this;
        const { data } = await this.$api.getAttrInfoList(
          value1,
          value2,
          value3
        );
        this.attrInfoList = data;
      } catch (e) {}
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
